package com.sd.repay.controller;

import com.sd.repay.annotation.MerAuthStatusCheck;
import com.sd.repay.common.CommonResult;
import com.sd.repay.common.Constants;
import com.sd.repay.pojo.PayOrder;
import com.sd.repay.service.*;
import com.sd.repay.utils.DateUtil;
import com.sd.repay.utils.JsonUtil;
import com.sd.repay.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by sd on 2018/08/21
 * 通道回调处理
 */
@Controller
@RequestMapping(value = "/acqBackMgr")
public class AcqBackController {

    private static final Logger log = LoggerFactory.getLogger(AcqBackController.class);

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private CardManageService cardManageService;
    @Autowired
    private RepayPlanService planService;
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private WithdrawOrderService withdrawOrderService;
    @Autowired
    private MerTokenService merTokenService;

    /**
     * 畅捷订单回调接收
     *
     * @return
     */
    @MerAuthStatusCheck
    @RequestMapping(value = "/cjPayOrderBack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public CommonResult cjPayOrderBack(@RequestParam Map<String, String> params) {

        log.info("畅捷交易订单回调接收参数[{}]", new Object[]{JsonUtil.objectToJson(params)});

        String respCode = params.get("respCode");
        String respMsg = params.get("respMsg");
        String orderNo = params.get("orderNo");
        String orderStatus = params.get("orderStatus");
        String payTime = params.get("payTime");

        try {
            if (StringUtil.isBlank(respCode, orderNo, orderStatus)) {
                return CommonResult.build(403, "【respCode】【orderNo】【orderStatus】必要信息不能有空值");
            }
            payTime = StringUtils.isBlank(payTime) ? DateUtil.getDateTime() : payTime;
            respMsg = "订单异步通知：" + (StringUtils.isBlank(respMsg) ? "" : respMsg);

            PayOrder order = payOrderService.getPayOrderByOrderNo(orderNo);
            log.info("畅捷交易异步通知原始订单号[{}]对应的订单信息[{}]", new Object[]{orderNo, JsonUtil.objectToJson(order)});
            if (null == order) {
                return CommonResult.build(403, "原始订单不存在");
            }
            String origTransStatus = order.getTransStatus();

            if (Constants.ORDER_TRANS_STATUS_UNKNOW.equals(origTransStatus)) {
                int updateRow = 0;
                if ("2".equals(orderStatus)) {
                    //成功
                    updateRow = payOrderService.writeBackPayOrderInfo(orderNo, null, payTime, Constants.ORDER_TRANS_STATUS_SUCCESS, Constants.ORDER_TRANS_STATUS_UNKNOW, respCode, respMsg);
                    log.info("畅捷交易异步通知原始订单号[{}]状态成功影响的行数[{}]", new Object[]{orderNo, updateRow});

                } else if ("5".equals(respCode)) {
                    //失败
                    updateRow = payOrderService.writeBackPayOrderInfo(orderNo, null, null, Constants.ORDER_TRANS_STATUS_FAIL, Constants.ORDER_TRANS_STATUS_UNKNOW, respCode, respMsg);
                    log.info("畅捷交易异步通知原始订单号[{}]状态失败影响的行数[{}]", new Object[]{orderNo, updateRow});

                } else {
                    //还是未知不作处理
                }

                if (updateRow > 0) {
                    //处理后续业务
                    updateRow = payOrderService.updatePayOrderNotifyStatus(orderNo, Constants.IS_NEED_NOTIFY);
                    log.info("畅捷交易异步通知原始订单号[{}]通知成功，修改需要通知状态影响的行数updateRow=[{}]", new Object[]{orderNo, updateRow});
                }
            }
            return CommonResult.ok();

        } catch (Exception e) {
            e.printStackTrace();
            log.info("畅捷交易订单异步通知[{}]异常[{}]", new Object[]{orderNo, e});
            return CommonResult.build(403, "操作失败，请稍候再试");
        }
    }
}
